import * as sp from '../common/sp_config.js'

// import encode from './encode.js'

import {
	initCacheUserInfo
} from '../model/BaseModel.js'

import * as consts from '../consts.js'

class flyrise {

	static initUrlParams() {
		let href = window.location.href
		let st = this.getQueryVariable(href, "st")
		if (!st) {
			return
		}
		let stHead = this.getQueryVariable(href, "stHead") || "Bearer"
		let rt = this.getQueryVariable(href, "rt")
		let ei = this.getQueryVariable(href, "ei")
		let lv = this.getQueryVariable(href, "lv")
		// let token = st ? encode.decodeToken(st) : ""
		this.save(sp.token, stHead + " " + st)
		this.save(sp.tokenValue, st)
		this.save(sp.loginVersion, lv)
		this.save(sp.expiresIn, (new Date().getTime() / 1000) + ei)
		initCacheUserInfo((isSuccess, res) => {

		}) //缓存用户信息
		console.log('>>>token:::--initUrlParams', flyrise.getToken());
	}

	static getQueryVariable(query, variable) {
		var rooms = query.split('?');
		for (var i = 0; i < rooms.length; i++) {
			var pair = rooms[i].split('=');
			if (pair[0] == variable) {
				return (pair[1].split('&'))[0]
			}
		}
		//或者拼接&后面的值
		var vars = query.split('&')
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split('=')
			if (pair[0] == variable) {
				return pair[1]
			}
		}
	}

	//全局获取
	static get(key) {
		return uni.getStorageSync(key)
	}

	//全局存储
	static save(key, value) {
		uni.setStorageSync(key, value)
	}

	//是否为凌云微应用
	static isMicro() {
		try {
			let mobile = this.get(sp.mobile)
			return mobile == 'android' || mobile == 'ios'
		} catch (e) {}
		return false
	}

	//微信appid
	static getWechatId() {
		return consts.WX_APPID || ''
	}

	//获取 token
	static getToken() {
		let token = ''
		try {
			token = this.get(sp.token)
		} catch (e) {}
		return this.toUpperCase(token)
	}

	//小写转大写
	static toUpperCase(name) {
		return (name?.charAt(0)?.toUpperCase() || "") + name?.slice(1) || ""
	}

	//获取 baseUrl
	static getBasePath() {
		let basePath = ''
		try {
			basePath = uni.getStorageSync(sp.basePath)
			if (!basePath) {
				basePath = consts.BASE_URL
			}
		} catch (e) {
			basePath = consts.BASE_URL
		}
		return basePath
	}

	/**
	 * @description 彻底关闭当前小程序
	 */
	static finish(info) {
		console.log("sendNativeEvent-----------finishs");
		//#ifdef APP-PLUS
		plus.runtime.quit()
		this.sendNative("mp_micro_finish", "");
		//#endif
	}

	//给原生发送事件
	static sendNative(event, value) {
		// #ifdef APP-PLUS
		uni.sendNativeEvent(event, value, (e) => {
			console.log("sendNativeEvent-----------回调" + JSON.stringify(e));
		});
		// #endif
	}

	// 重定向js
	static redirect(_this, e, showTitle = true) {
		if (!e || !e.action) {
			return false
		}
		// #ifdef APP-PLUS
		// 判断是否为启动直达页面
		if (e.action === "redirect" && showTitle) {
			const currentWebview = _this.$scope.$getAppWebview();
			currentWebview.setStyle({
				popGesture: 'none', // 取消手势返回
				titleNView: {
					autoBackButton: false // 取消默认返回按钮
				}
			});
		}
		// #endif
		return e.action == "redirect"
	}

	//存储壳传递的参数
	static shellData(args) {
		this.clearStorage()
		if (!args) {
			return
		}

		try {
			console.log("--->>>args:", JSON.stringify(args))
			var isDebug = args && args.isDebug;
			if (isDebug && (isDebug == 1 || isDebug == true)) {
				this.initDebug()
			}

			console.log(JSON.stringify(args))
			this.save(sp.mobile, args.mobile)
			this.save(sp.token, args.token)
			this.save(sp.tokenValue, args.tokenValue)
			this.save(sp.refreshToken, args.refresh_token)
			this.save(sp.userName, args.userName)
			this.save(sp.userId, args.userId)
			this.save(sp.staffId, args.staffId)
			this.save(sp.entId, args.entId)
			this.save(sp.entName, args.entName)
			this.save(sp.parkId, args.parkId)
			this.save(sp.parkName, args.parkName)
			this.save(sp.phone, args.phone)
			this.save(sp.deviceCode, args.deviceId)
			this.save(sp.basePath, args.basePath)
			this.save(sp.suiteCode, args.suiteCode)
			this.save(sp.sensorsUrl, args.sensorsUrl)
			this.save(sp.ailAppId, args.ailAppId)
			this.save(sp.wxAppId, args.wxAppId)
			this.save(sp.stHead, args.stHead)
			this.save(sp.st, args.st)
			this.save(sp.version, args.version)
			this.save(sp.loginVersion, args.loginVersion)
		} catch (e) {
			console.log('---->>>app-Shortcut-exception: ' + e);
		}
	}

	static logoutRelogin() {
		this.save(sp.parkId, "")
		this.save(sp.parkName, "")
		this.save(sp.userName, "")
		this.save(sp.entId, "")
		this.save(sp.entName, "")
		this.save(sp.staffId, "")
		this.save(sp.userId, "")
		this.save(sp.token, "")
		this.save(sp.refreshToken, "")
		this.save(sp.isLogin, false)
		uni.reLaunch({
			url: "/pages/login/index"
		})
	}

	static clearStorage() {
		try {
			this.save(sp.basePath, "")
		} catch (e) {}
	}
}

export default flyrise